package com.spdb.spdbfl.utils;

import com.spdb.spdbfl.config.PathDefined;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.PropertiesConfigurationLayout;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 处理config
 */
public class ConfigProperty {

    public static Map<String, String> propMap = new HashMap();

    public ConfigProperty() {
    }

    /**
     * 更新config.properties
     */
    public static void updateProperty(String key, String value) {
        PathDefined pathDefined = new PathDefined();
        String configPath = pathDefined.getConfigPath();

        File file = new File(configPath);
        PropertiesConfiguration config = new PropertiesConfiguration();
        PropertiesConfigurationLayout layout = config.getLayout();
        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file), "GBK");
            layout.load(config, inputStreamReader);
            config.setProperty(key, value);
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file, false), "GBK");
            layout.save(config, outputStreamWriter);
        } catch (Exception e) {
            System.err.println("config.properties更新异常");
            e.printStackTrace();
        } finally {
            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取配置信息
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        InputStreamReader input = null;
        try {
            PathDefined pathDefined = new PathDefined();
            String configPath = pathDefined.getConfigPath();
            File file = new File(configPath);
            input = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
//            input = new InputStreamReader(Objects.requireNonNull(ConfigProperty.class.getClassLoader().getResourceAsStream("config.properties")), StandardCharsets.UTF_8);
        } catch (Exception var36) {
            var36.printStackTrace();
        }
        Properties prop = new Properties();
        if (input != null) {
            try {
                prop.load(input);
                Set<Object> keyset = prop.keySet();

                for (Object object : keyset) {
                    String propKey = object.toString().trim();
                    String propValue = prop.getProperty(propKey).toString().trim();
                    propMap.put(propKey, propValue);
                }
            } catch (IOException var39) {
                var39.printStackTrace();
            } finally {
                try {
                    input.close();
                } catch (IOException var33) {
                    var33.printStackTrace();
                }
            }
        }
        System.out.println("配置propMap:" + propMap);
        String value = (String) propMap.get(key);
//        System.out.println("value:" + value);
        return value;
    }


    public static void main(String[] args) {
        updateProperty("liumToken", "3333");
        getProperty("liumToken");
    }


}

